﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SalaryMS
{
    public partial class EmpUpdateUI : Form
    {
        public EmpUpdateUI()
        {
            InitializeComponent();
            btnDeleteEmp.Enabled = false;
            btnUpdateEmp.Enabled = false;
        }

        private void btnEmpSearch_Click(object sender, EventArgs e)
        {
            if (txtEmpId.Text.Trim().Length > 0)
            {
                String sql = "SELECT EmpId,EmpName,BankAccount,Remark FROM Emplyee WHERE EmpId='" + txtEmpId.Text.Trim() + "'";
                DataTable dt = DBSession.ExecuteDataTable(sql, null);
                if (dt != null && dt.Rows.Count > 0)
                {
                    txtEmpId.Enabled = false;
                    btnDeleteEmp.Enabled = true;
                    btnUpdateEmp.Enabled = true;
                    this.AcceptButton = btnUpdateEmp;
                    txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                    txtBankAccount.Text = dt.Rows[0]["BankAccount"].ToString();
                    txtRemark.Text = dt.Rows[0]["Remark"].ToString();

                    dgv_EmpInfo.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("不能找到数据，请检查你的员工ID号");
                }
            }
            else
            {
                MessageBox.Show("员工ID号不能为空");
            }
        }

        private void btnDeleteEmp_Click(object sender, EventArgs e)
        {
            String sql = "DELETE FROM Emplyee WHERE EmpId='" + txtEmpId.Text.Trim() + "'";
            int num = DBSession.ExecuteNonQuery(sql, null);
            if (num > 0)
            {
                MessageBox.Show("删除用户成功！");
                txtEmpId.Text = "";
                txtEmpName.Text = "";
                txtBankAccount.Text = "";
                txtRemark.Text = "";
                txtEmpId.Focus();
                txtEmpId.Enabled = true;
                btnDeleteEmp.Enabled = false;
                btnUpdateEmp.Enabled = false;
                this.AcceptButton = btnEmpSearch;
                dgv_EmpInfo.DataSource = null;
            }
            else
            {
                MessageBox.Show("删除用户失败，请重试！");
            }
        }

        private void btnUpdateEmp_Click(object sender, EventArgs e)
        {
            String EmpId = txtEmpId.Text.ToString();
            String EmpName = txtEmpName.Text.ToString();
            String BankAccount = txtBankAccount.Text.ToString();
            String Remark = txtRemark.Text.ToString();
            String CreateDate = DateTime.Now.ToString();
            if (EmpId.Length > 0 && EmpName.Length > 0 && BankAccount.Length > 0)
            {
                String sql = "UPDATE Emplyee "
                    +" SET EmpName='"+EmpName+"',BankAccount='"+BankAccount+"',CreateDate='"+CreateDate+"',Remark='"+Remark+"'"
                    +"WHERE EmpId='"+EmpId+"'";
                int count = DBSession.ExecuteNonQuery(sql, null);
                if (count > 0)
                {
                    sql = "SELECT EmpId,EmpName,BankAccount,CmpName,Emplyee.CreateDate,Emplyee.Remark FROM Emplyee "
                        + " LEFT JOIN Company on Emplyee.CmpId = Company.CmpId "
                        + " ORDER BY Emplyee.CreateDate DESC";
                    DataTable dtResult = DBSession.ExecuteDataTable(sql, null);
                    dgv_EmpInfo.DataSource = dtResult;
                }

                //clear control value.
                txtEmpId.Text = "";
                txtEmpName.Text = "";
                txtBankAccount.Text = "";
                txtRemark.Text = "";
                txtEmpId.Focus();
                txtEmpId.Enabled = true;
                btnDeleteEmp.Enabled = false;
                btnUpdateEmp.Enabled = false;
                this.AcceptButton = btnEmpSearch;

            }
            else
            {
                MessageBox.Show("除备注外，其余信息不能为空！");
            }
        }
    }
}
